/*
package com.sunplus.system.controller.wechat;

import com.mxixm.fastboot.weixin.annotation.WxController;
import com.sunplus.common.core.controller.BaseController;
import com.sunplus.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

@WxController
@RequestMapping("/wechat/")
public class WeChatCallBackController extends BaseController {


    @Value("${wx.token}")
    private String token;

    @GetMapping(value = "callback")
    public void reveiceMsg(HttpServletRequest req, HttpServletResponse resp) {
        String signature = req.getParameter("signature");
        String timestamp = req.getParameter("timestamp");
        String nonce = req.getParameter("nonce");
        String echostr = req.getParameter("echostr");
        System.out.println("微信加密签名" + signature);
        System.out.println("时间戳" + timestamp);
        System.out.println("随机数" + nonce);
        System.out.println("随机字符串" + echostr);
        Boolean flag = checkSignacheckSignatureture(signature, timestamp, nonce);
        if (flag) {
            try (PrintWriter out = resp.getWriter();) {
                if (StringUtils.isNoneBlank(echostr)) {
                    out.write(echostr);
                    out.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public boolean checkSignacheckSignatureture(String signature, String timestamp, String nonce) {
        //1.将token,timestamp,nonce三个字符装入数组
        String[] arr = new String[]{token, timestamp, nonce};
        //实现按字典顺序排序
        Arrays.sort(arr);
        //2.将排序后的数组拼接成一个字符串
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            builder.append(arr[i]);
        }
        System.out.println("拼接后的字符串：" + builder);
        MessageDigest md = null;
        String result = null;
        try {
            //3.对拼接后的字符串进行sha1加密
            md = md.getInstance("SHA-1");
            byte[] digest = md.digest(builder.toString().getBytes());
            //将字节数组转成密文字符串
            result = byteToString(digest);
            System.out.println("加密后的字符串" + result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        //4.将加密后的字符串与微信服务器传过来的签名进行对比
        return result != null ? (result.equals(signature.toUpperCase())) : false;
    }

    */
/**
     * 实现将字节数组转换成为十六进制字符串
     *
     * @param str
     * @return
     *//*

    private static String byteToString(byte[] str) {
        String strDigest = "";
        for (int i = 0; i < str.length; i++) {
            strDigest += byteToHexStr(str[i]);
        }
        return strDigest;
    }

    */
/**
     * 实现将每一个字节转换成十六进制字符串
     *
     * @param mByte
     * @return
     *//*

    private static String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] temp = new char[2];
        temp[0] = Digit[(mByte >>> 4) & 0X0F];
        temp[1] = Digit[mByte & 0X0F];
        String s = new String(temp);//构建一个字符串
        return s;
    }
}
*/
